﻿using System.Web.UI.WebControls;

namespace ETestIT.UI
{
    using System;
    using Core;
    using Core.Infrastructure;
    using Presentation;
    using Presentation.Storage;
    using SharpArch.Core.PersistenceSupport;

    public partial class JobTitles : PageBase<JobTitles>
    {
        private const string EditedjobTitleKey = "editedJobTitle";
        private ISessionProvider sessionStorage;
        private IRepository<JobTitle> jobTitleRepository;
        protected void Page_Load(object sender, EventArgs e)
        {
            jobTitleAddEdit.Update += JobTitleAddEditUpdate;
            jobTitleRepository = IoC.Resolve<IRepository<JobTitle>>();
            sessionStorage = IoC.Resolve<ISessionProvider>();

            if (!IsPostBack)
                GetAllJobTitles();
            
        }
        public void GetAllJobTitles()
        {
            gvJobTitles.DataSource = jobTitleRepository.GetAll();
            gvJobTitles.DataBind();
        }

        protected void LnkEditCommand(object sender, CommandEventArgs e)
        {
            var jobTitleId = int.Parse(e.CommandArgument.ToString());

            switch (e.CommandName)
            {
                case "JobTitleEdit":
                    {
                        jobTitleAddEdit.SetModel(jobTitleRepository.Get(jobTitleId));
                        jobTitleAddEdit.Visible = Visible;
                        sessionStorage.Set(EditedjobTitleKey, jobTitleId);
                        GetAllJobTitles();
                        break;
                    }
                case "JobTitleDelete":
                    {
                        var jobTitle = jobTitleRepository.Get(jobTitleId);
                        With.Transaction(()=> jobTitleRepository.Delete(jobTitle));
                        GetAllJobTitles();
                        break;
                    }
               
                default:
                    jobTitleAddEdit.Visible = false;
                    break;
            }
        }

        protected void BtnCreateNewJobTitle_Clicked(object sender, EventArgs e)
        {
            jobTitleAddEdit.Visible = Visible;
        }

        public void JobTitleAddEditUpdate(object sender, EventArgs e)
        {
            var theJobTitle = sessionStorage.Contains(EditedjobTitleKey) ? 
                jobTitleRepository.Get(sessionStorage.Get<int>(EditedjobTitleKey)): new JobTitle();

            if (jobTitleAddEdit.IsInputValide())
            {
                jobTitleAddEdit.UpdateModelFromUserInput(theJobTitle);
                With.Transaction(()=> jobTitleRepository.SaveOrUpdate(theJobTitle));
                Dialog.Information = "The Job Title has been updated successfully";
                jobTitleAddEdit.Visible = false;
                GetAllJobTitles();

                if (sessionStorage.Contains(EditedjobTitleKey))
                    sessionStorage.Remove(EditedjobTitleKey);
            }

        }
    }
}